package com.example.ailiyuncloud.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ailiyuncloud.dto.CommoditySpuDTO;
import com.example.ailiyuncloud.mapper.CommoditySpuMapper;
import com.example.ailiyuncloud.pojo.CommodityErrorPojo;
import com.example.ailiyuncloud.pojo.Result;
import com.example.ailiyuncloud.service.CommoditySpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.example.ailiyuncloud.service.Impl.CommoditySkuServiceImpl.generateId;

/**
 * @Author: lqj
 * @Date: 2024/05/26/16:52
 * @Description:
 */
@Service
public class CommoditySpuServiceImpl implements CommoditySpuService {

    private static final String SPU = "SPU-";

    @Autowired
    private CommoditySpuMapper commoditySpuMapper;

    @Autowired
    private RedisTemplate<String, Object> stringRedisTemplate;

    @Override
    public Result<?> addCommoditySpuService(List<CommoditySpuDTO> CommoditySkuDtoList) {
        if (CommoditySkuDtoList.size() > 200) {
            return Result.error("单次SPU最多为200");
        }
        List<String> collect = commoditySpuMapper.selectList(Wrappers.<CommoditySpuDTO>lambdaQuery()
                        .in(CommoditySpuDTO::getCommodityName, CommoditySkuDtoList.stream()
                                .map(CommoditySpuDTO::getCommodityName).collect(Collectors.toList()))
                        .eq(CommoditySpuDTO::getDelIs, "0"))
                .stream().map(CommoditySpuDTO::getCommodityName)
                .collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>(16);
        List<CommodityErrorPojo> commodityErrorPojoList = new ArrayList<>(16);
        CommoditySkuDtoList.forEach(commoditySkuDto -> {
            String commodityName = commoditySkuDto.getCommodityName();
            if (collect.contains(commodityName)) {
                commodityErrorPojoList.add(new CommodityErrorPojo().commodityErrorPojo(commodityName, "SPU重复添加"));
                return;
            }
            commoditySkuDto.setSpuId(SPU + generateId());
            commoditySpuMapper.insert(commoditySkuDto);
        });
        //插入数据后从数据库中加载最新的10条数据
        refreshSpuRedis();
        if (commodityErrorPojoList.size() > 0) {
            map.put("info", "部分spu添加成功");
            map.put("errorInfo", commodityErrorPojoList);
            return Result.error(map);
        } else {
            map.put("info", "spu添加成功");
        }
        return Result.OK(map);
    }

    @Override
    public Result<?> queryCommoditySpuService(Page page, CommoditySpuDTO CommoditySkuDtoList) {
        Map<String, Object> map = new HashMap<>(16);
        //判断是否查询第二页数据，如是查数据库。
        //todo 需要补上1页100条判断
        List<Object> commoditySpuDTOListInfo = new ArrayList<>(16);
        if (page.getCurrent() > 1) {
            commoditySpuDTOListInfo.addAll(commoditySpuMapper.getCommoditySpuDTOListInfo(page, CommoditySkuDtoList));
        } else {
            //先查询缓存中是否存在
            commoditySpuDTOListInfo.addAll(stringRedisTemplate.opsForList().range(SPU, 0, 10));
            if (ObjectUtils.isEmpty(commoditySpuDTOListInfo)) {
                //查询数据库是否存在数据，如不存在则将空数据放缓存中过期时间为一分钟
                commoditySpuDTOListInfo.addAll(commoditySpuMapper.getCommoditySpuDTOListInfo(page, CommoditySkuDtoList));
                //数据库中存在，缓存不存在则刷新一次缓存
                refreshSpuRedis();
            }
        }
        map.put("info", commoditySpuDTOListInfo);
        map.put("current", page.getCurrent());
        map.put("size", page.getSize());
        return Result.OK(map);
    }

    public void refreshSpuRedis() {
        List<CommoditySpuDTO> commoditySpuDTOList = commoditySpuMapper.getCommoditySpuDTOList(10);
        stringRedisTemplate.delete(SPU);
        stringRedisTemplate.opsForList().rightPushAll(SPU, commoditySpuDTOList);
    }


}
